//
//  LMEditViewController.m
//  20.私人通讯录
//
//  Created by 张青花 on 15/8/27.
//  Copyright (c) 2015年 李明. All rights reserved.
//

#import "LMEditViewController.h"
#import "LMPhone.h"

@interface LMEditViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *editBtn;
@end

@implementation LMEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(changeBtn) name:UITextFieldTextDidChangeNotification
                 object:self.userField];
    [center addObserver:self selector:@selector(changeBtn) name:UITextFieldTextDidChangeNotification
                 object:self.pwdField];
    
    
    self.userField.text = self.phone.username;
    self.pwdField.text = self.phone.number;
    
}
//改变登录状态
- (void)changeBtn
{
    self.saveBtn.enabled = (self.userField.text.length>0&&self.pwdField.text.length);
}
- (IBAction)editBtnOnClick:(id)sender {
    
    if(self.userField.enabled && self.pwdField.enabled)
    {
        
        
        self.userField.enabled = NO;
        self.pwdField.enabled = NO;
        self.editBtn.title = @"编辑";
        self.saveBtn.hidden = YES;
        
        self.userField.text = self.phone.username;
        self.pwdField.text = self.phone.number;
    }
    else
    {
        
        self.userField.enabled = YES;
        self.pwdField.enabled = YES;
        
        [self.pwdField becomeFirstResponder];
        self.editBtn.title = @"取消";
        self.saveBtn.hidden = NO;
    }
        
                                  
}
- (IBAction)saveBtnOnClick:(id)sender {
    
    self.phone.username = self.userField.text;
    self.phone.number = self.pwdField.text;
    
    if ([self.deleage respondsToSelector:@selector(editViewController:phone:)]) {
        
        [self.deleage editViewController:self phone:self.phone];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
